package com.yyb.boot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/28
 *
 * 注意: WebMvcConfigurerAdapter这个类,@deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
 *  possible by a Java 8 baseline) and can be implemented directly without the
 *  need for this adapter.
 *  WebMvcConfigurerAdapter这个类从5.0以后就过失了,5.0之后直接实现WebMvcConfigurer接口就行.
 *
 *  这是一种全局配置的实现方式,一般都是这种解决办法.
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //针对的映射
        registry.addMapping("/**")
                //针对的origin域名
                .allowedOrigins("*")
                //针对的方法
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                //是否允许发送Cookie
                .allowCredentials(true)
                //从预检请求得到相应的最大时间,默认30分钟
                .maxAge(3600)
                //针对的请求头
                .allowedHeaders("*");
    }

}
